﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

namespace PlurkApi
{
    /// <summary>
    /// user information
    /// </summary>
    public class user_info
    {
        /// <summary>
        /// The non-unique display name of the user, for example Amir S. Only set if it's non empty.
        /// </summary>
        public string display_name { get; set; }

        /// <summary>
        /// Can be not_saying, single, married, divorced, engaged, in_relationship, complicated, widowed, open_relationship
        /// </summary>
        public string relationship { get; set; }

        /// <summary>
        /// The profile title of the user.
        /// </summary>
        public string page_title { get; set; }

        /// <summary>
        /// The unique nick_name of the user, for example amix
        /// </summary>
        public string nick_name { get; set; }

        /// <summary>
        /// The user's location, a text string, for example Aarhus Denmark.
        /// </summary>
        public string location { get; set; }

        /// <summary>
        /// The user's full name, like Amir Salihefendic.
        /// </summary>
        public string full_name { get; set; }
        public string timezone { get; set; }
        
        /// <summary>
        /// User's karma value.
        /// </summary>
        public double karma { get; set; }

        public int uid { get; set; }

        /// <summary>
        /// Specifies what the latest avatar (profile picture) version is.
        /// </summary>
        public int? avatar { get; set; }

        /// <summary>
        /// If 1 then the user has a profile picture, otherwise the user should use the default.
        /// </summary>
        public int has_profile_image { get; set; }

        /// <summary>
        /// 1 is male, 0 is female.
        /// </summary>
        public int gender { get; set; }

        /// <summary>
        /// How many friends has the user recruited.
        /// </summary>
        public int recruited { get; set; }

        /// <summary>
        /// The unique user id.
        /// </summary>
        public int id { get; set; }

        /// <summary>
        /// The user's birthday.
        /// </summary>
        public DateTime date_of_birth { get; set; }
        
        public user_info(string jsonString)
        {
            var obj = JObject.Parse(jsonString);
            if (obj.Count <= 0) { return; }

            display_name = (obj["display_name"] == null) ? string.Empty : (string)obj["display_name"];
            relationship = (obj["relationship"] == null) ? string.Empty : (string)obj["relationship"];
            page_title = (obj["page_title"] == null) ? string.Empty : (string)obj["page_title"];
            nick_name = (obj["nick_name"] == null) ? string.Empty : (string)obj["nick_name"];
            location = (obj["location"] == null) ? string.Empty : (string)obj["location"];
            
            full_name = (obj["full_name"] == null) ? string.Empty : (string)obj["full_name"];
            timezone = (obj["timezone"] == null) ? string.Empty : (string)obj["timezone"];
            karma = (obj["karma"] == null) ? 0.0 : double.Parse(obj["karma"].ToString());

            uid = (obj["uid"] == null) ? 0 : (int)obj["uid"];
            avatar = (obj["avatar"] == null) ? null : (int?)obj["avatar"];
            has_profile_image = (obj["has_profile_image"] == null) ? 0 : (int)obj["has_profile_image"];
            gender = (obj["gender"] == null) ? 0 : (int)obj["gender"];
            recruited = (obj["recruited"] == null) ? 0 : (int)obj["recruited"];
            id = (obj["id"] == null) ? 0 : (int)obj["id"];

            if (obj["date_of_birth"] != null && obj["date_of_birth"].ToString() != "null") { date_of_birth = DateTime.Parse(obj["date_of_birth"].ToString().Replace("\"", "")); }
        }

        public string getAvatarUrl(AvatarSize size)
        {
            if (has_profile_image == 0)
            {
                return string.Format("http://www.plurk.com/static/default_{0}.gif", uid);
            }
            
            if (has_profile_image == 1 && avatar != null)
            {
                return string.Format("http://avatars.plurk.com/{0}-{1}{2}.gif", uid, size, avatar);
            }
            
            if (has_profile_image == 1 && avatar == null)
            {
                return string.Format("http://avatars.plurk.com/{0}-{1}.jpg ", uid, size);
            }

            return string.Empty;
        }

        public override string ToString()
        {
            return id.ToString();
        }

        public override bool Equals(object obj)
        {
            if (obj is user_info)
            {
                var u = (user_info)obj;
                return (u.id == id);
            }
            return false;
        }

        public override int GetHashCode()
        {
            return ToString().GetHashCode();
        }
    }

    public class user_infos : List<user_info>
    {
    }

    public enum AvatarSize
    {
        small, medium, big
    }
}

